#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

typedef int DateType;
typedef struct DoubleLink
{
    DateType data;
    DoubleLink *prior;
    DoubleLink *next;
} DoubleLink;

// 初始化双向链表节点
DoubleLink *init(DateType d)
{
    DoubleLink *head = (DoubleLink *)malloc(sizeof(DoubleLink));
    head->data = d;
    head->next = NULL;
    head->prior = NULL;
    return head;
}
// 初始化双向循环链表
DoubleLink *initCriculate(DateType date)
{
    DoubleLink *head = (DoubleLink *)malloc(sizeof(DoubleLink));
    head->data = date;
    head->next = head;
    head->prior = head;
    return head;
}
// 双向循环链表头插元素
DoubleLink *insertCriculateHead(DoubleLink *head, DoubleLink *p)
{
    p->next = head->next;
    p->prior = head;
    head->next = p;
}

// 双向循环链表头删元素
DoubleLink *deleteCriculateHead(DoubleLink *head, DoubleLink *p)
{
    p->next = head->next;
    p->prior = head;
    head->next = p;
}
//双向循环链表尾删元素
DoubleLink *deleteCriculateTail(DoubleLink *head)
{
    DoubleLink *current=head->next;
    head->next=current->next;
    current->next->prior=head;
}

// 双向循环链表尾插元素
DoubleLink *insertCriculateTail(DoubleLink *head, DoubleLink *p)
{
    head->prior->next = p;
    p->prior = head->prior;
    p->next = head;
    head->prior = p;
}
// 在指定位置插入
/**
 * 链表
 * 元素
 * 位置
 */
void insert(DoubleLink *head, DoubleLink *p, int n)
{
    DoubleLink *current = head;
    int cur_i = 0;
    while (current && cur_i < (n - 1)) // 当前结点有效，且不是插入位置的前一个结点，就后移一个
    {
        current = current->next;
        cur_i++;
    }
    if (!current) // 当前结点无效，说明已经移动到最后
    {
        printf("[%s %d]error din't have No.%d\n", __FUNCTION__, __LINE__, n);
        return; // 链表没有 n 那么长
    }
    p->prior = current;
    p->next = current->next;
    if (current->next)
    {
        current->next->prior = p;
    }
    current->next = p;
}
// 链表打印
void printLinkList(DoubleLink *head)
{
    DoubleLink *current = head->next;
    int i = 1;
    while (current != NULL)
    {
        printf("当前节点为：%d,节点元素为：%c \n", i, current->data);
        current = current->next;
        i++;
    }
}

int main()
{
    char choise;
    DoubleLink *head;
    DoubleLink *p;
    char exit_choice;
    char date = '1';

    int n;
    while (1)
    {
        printf("请选择操作 \n");
        printf("C. 初始化 \n");
        printf("I. 指定位置插入 \n");
        printf("P. 打印 \n");
        printf("E. 退出 \n");
        scanf(" %c", &choise); // 加上空格忽略换行符
        switch (choise)
        {
        case 'C':
            printf("你选择了初始化双向链表 \n");
            head = init(date);
            break;
        case 'P':
            printf("你选择了打印双向链表 \n");
            printLinkList(head);
            break;
        case 'I':
            printf("你选择了指定位置插入双向链表 \n");
            printf("请输入你选择的指定位置： \n");
            scanf(" %d", &n);
            printf("请输入你插入的元素： \n");
            scanf(" %c", &date);

            p = init(date);
            insert(head, p, n);
            break;
        case 'E':
            printf("您确定要退出吗？按E键确认退出，按其他键返回上级菜单。\n");
            scanf(" %c", &exit_choice); // 加上空格忽略换行符
            if (exit_choice == 'E' || exit_choice == 'e')
            {
                printf("谢谢使用，再见！\n");
                return 0;
            }
            break;
        default:
            break;
        }
    }
}